package zhenniu.best.webview.zhenniuwebview.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;


import com.blankj.utilcode.util.PermissionUtils;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnViewClickListener;

import zhenniu.best.webview.zhenniuwebview.R;
import zhenniu.best.webview.zhenniuwebview.ZhenNiu;



public class BaseActivity extends AppCompatActivity {

    protected Activity mActivity; // 给子类用的，避免使用 XXXActivity.this 这种写法
    private PermissionTDListener mPermissionTDListener;
    //判断是否可以修改
    private boolean mStateEnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //屏幕分辨率
//        DisplayMetrics metrics = new DisplayMetrics();
//        Display display = getWindowManager().getDefaultDisplay();
//        display.getMetrics(metrics);
//        LogUtils.eTag("Metrics", "display is " + metrics.widthPixels + "*" + metrics.heightPixels);


    }

    /**
     * 显示提示对话框
     */
    public  void showTipsDialog() {

        //初始化TDialog相关逻辑
        new TDialog.Builder(getSupportFragmentManager())
                .setLayoutRes(R.layout.dialog_permission_tip)   //设置弹窗展示的xml布局
                .setScreenWidthAspect(this, 0.8f)   //设置弹窗宽度
                .setGravity(Gravity.CENTER)
                .setDimAmount(0.7f)
                .setCancelableOutside(false)    //弹窗是否可以取消
                .addOnClickListener(R.id.tv_cancel, R.id.tv_sure)   //添加进行点击控件的id
                .setOnViewClickListener(new OnViewClickListener() {   //View控件点击事件回调
                    @Override
                    public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                        switch (view.getId()) {
                            case R.id.tv_cancel:
                                tDialog.dismiss();
                                if (mPermissionTDListener != null) {
                                    mPermissionTDListener.permissionCancel();
                                }
                                break;
                            case R.id.tv_sure:
                                tDialog.dismiss();
                                PermissionUtils.launchAppDetailsSettings();
                                if (mPermissionTDListener != null) {
                                    mPermissionTDListener.permissionSure();
                                }
                                break;
                            default:
                                break;
                        }
                    }
                })
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (mPermissionTDListener != null) {
                            mPermissionTDListener.permissionDismiss();
                        }
                    }
                })
                .create()   //创建TDialog
                .show();   //展示
    }

    public void setPermissionTDListener(PermissionTDListener permissionTDListener){
        this.mPermissionTDListener = permissionTDListener;
    }

    public interface PermissionTDListener{
        void permissionCancel();
        void permissionSure();
        void permissionDismiss();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ZhenNiu.removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onStop() {
        super.onStop();
    }

}
